package com.youmingxing.activity.selfInfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.adpter.TopicAdapter;
import com.youmingxing.bean.TopicInfo;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.view.RefreshListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class TopicActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    
    private RefreshListView listView;
    private List<TopicInfo> mTopicInfos = new ArrayList<>();
    private TopicAdapter mAdapter;

    public static int RESULTCODE_TOPIC = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_topic);
        setTopbarName("话题");
        listView = (RefreshListView) findViewById(R.id.list_choose_topic);
        listView.setOnItemClickListener(this);
        AllStarApi.getTopicEvents(topicHandler);
    }


    BaseJsonHandler topicHandler = new BaseJsonHandler(this) {
        public void onSuccessWithData(JSONObject response) {
            listView.endLoadMore();
            showErrorPage(false);
            JSONArray data = response.optJSONArray("data");
            System.out.println("data = " + data);
            if (data.length() == 0) {
                listView.noData("当前没有#活动");
            }
            if (data.length() < 20) {
                listView.noMoreData();
            }
            for (int i = 0; i < data.length(); i++) {
                JSONObject jsonObject = data.optJSONObject(i);
                TopicInfo topicInfo = new TopicInfo();
                topicInfo.setImg(jsonObject.optString("img"));
                topicInfo.setTitle(jsonObject.optString("title"));
                topicInfo.setDescription(jsonObject.optString("description"));
                mTopicInfos.add(topicInfo);
            }
            mAdapter = new TopicAdapter(TopicActivity.this,mTopicInfos);
            listView.setAdapter(mAdapter);

        }

        @Override
        public void onFail(int statusCode, String statusText) {
            super.onFail(statusCode, statusText);
            listView.endLoadMore();
            showErrorPage(true);
        }
    };

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TopicInfo topicInfo = mTopicInfos.get(position);
        Intent intent=new Intent();
        intent.putExtra("topic_title", topicInfo.getTitle());
        setResult(RESULTCODE_TOPIC, intent);
        finish();
    }
}
